'''

改进灰色关联系数 （有提升）

'''
import numpy as np
from SBFI import func_sbf
def gra_imporve(S,data):
    label_need=data.keys()[:]
    data1=data[label_need].values
    [m,n]=data1.shape
    data2=data1.astype('float')
    data3=data2
    ymin=0.002
    ymax=1
    for j in range(0,n):
        d_max=max(data2[:,j])
        d_min=min(data2[:,j])
        data3[:,j]=(ymax-ymin)*(data2[:,j]-d_min)/(d_max-d_min)+ymin
    yk=0
    for l in range(0,m):
        yk+=data3[l:l+1,n-1]
    yk1=yk/m
    print(yk1)
    for i in range(0,n-1):
        xk = 0
        for j in range(0,m):
            xk+= data3[j:j + 1,i]
        data3[:,i]=np.abs(np.sqrt(np.square(data3[:,i]-xk/m))-np.sqrt(np.square(data3[:,n-1]-yk1)))
        print(data3[:,i])
    data4=data3[:,0:n-1]
    d_max=np.max(data4)
    d_min=np.min(data4)
    a=0.5
    data4=(d_min+a*d_max)/(data4+a*d_max)
    res=np.mean(data4, axis=0)
    sorted_nums = sorted(enumerate(res), key=lambda x: x[1])
    idx = [i[0] for i in sorted_nums]
    func_sbf(S, idx, data)

